package com.cookbook.animals;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.*;


public class Menu extends Activity {
	// Create variables to hold four buttons
	Button practice;
	Button solo;
	Button pair;
	Button history;
	
	// Declare a variable to hold user name
	String user;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        
        // Set layout
        setContentView(R.layout.menu);
        
        // Check if savedInstanceState is non-null
        if(savedInstanceState != null){
        	// If so get user name from bundle
        	user = savedInstanceState.getString("user");
        }
        
        // Else get string passed by previous activity
        else{
        	// Get user name and save it to user
        	user = this.getIntent().getStringExtra("user");
        }
        
        // Instantiate practice button
        practice = (Button) findViewById(R.id.practice);
        
        // Instantiate solo button
        solo = (Button) findViewById(R.id.solo_quiz);
        
        // Instantiate pair button
        pair = (Button) findViewById(R.id.pair_quiz);
        
        // Instantiate history button
        history = (Button) findViewById(R.id.history);
        
        // Create listener for practice button
    	practice.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Create intent for practice class
				Intent start_practice = new Intent(getApplicationContext(), Practice.class);
				
				// Start new activity using start_animals intent
				startActivity(start_practice);
			}
		});
    	
    	// Create listener for solo button
    	solo.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Create intent for animals class
				Intent start_animals = new Intent(getApplicationContext(), animals.class);
				
				// Put user into start_animals as an extra
				start_animals.putExtra("user", user);
				
				// Start new activity using start_animals intent
				startActivity(start_animals);
			}
		});
    	
    	// Create listener for pair button
    	pair.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Create intent for animals class
				Intent start_pair = new Intent(getApplicationContext(), Pair.class);
				
				// Put user into start_animals as an extra
				start_pair.putExtra("user", user);
				
				// Start new activity using start_animals intent
				startActivity(start_pair);
			}
		});
    	
    	// Create listener for history button
    	history.setOnClickListener(new View.OnClickListener() {
    		// Override onClick function
    		@Override
    		public void onClick(View v){
    			// Create new intent for history class
    			Intent start_history = new Intent(getApplicationContext(), History.class);
    			
    			// Put user into start_history as an extra
    			start_history.putExtra("user", user);
    			
    			// Start new activity using start_history intent
    			startActivity(start_history);
    		}
    	});
    }
    
    // Override onSavedInstanceState function
    @Override
    public void onSaveInstanceState(Bundle outState){
    	// Call super function
    	super.onSaveInstanceState(outState);
    	
    	// Put user into outState bundle
    	outState.putString("user", user);
    }
}
